package com.molyfun.parents.activity.common.listactivity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/28/16.
 */
public abstract class BaseListActivity<T> extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener,
        View.OnClickListener, IResponse{

    @InjectView(R.id.base_title_left)
    ImageView mBack;

    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;

    @InjectView(R.id.base_title)
    TextView mTitle;

    LinearLayoutManager mLayoutManager;

    protected int mCurrentPage = RestClient.DEFAULT_PAGE;
    protected AbstractListFactory mListFactory;
    protected ListAdapter mAdapter;
    private boolean mHasMoreData = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_list);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mTitle.setText(mListFactory.getTitle());
        mBack.setOnClickListener(this);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);
        mAdapter = new ListAdapter(this, mListFactory, getSupportFragmentManager());
        mRecyclerView.setAdapter(mAdapter);

        getData();

    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    @Override
    public void onResponse(List dataList) {
        if(dataList.size() > 0){
            mEmptyLayout.setVisibility(View.GONE);
            mHasMoreData = true;
            if(mCurrentPage == RestClient.DEFAULT_PAGE){
                mAdapter.refreshDataList(dataList);
            }else{
                mAdapter.addAll(dataList);
            }
            mAdapter.notifyDataSetChanged();
        }else{
            if(mCurrentPage == RestClient.DEFAULT_PAGE){
                mEmptyLayout.setVisibility(View.VISIBLE);
                mAdapter.clear();
            }
            mHasMoreData = false;
        }
    }

    private void getData(){
        mListFactory.getDataList(mCurrentPage, this);
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            getData();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }
}
